package com.qububu.emporium.goods.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.emporium.goods.dto.GoodsAboutDto;
import com.qububu.emporium.goods.dto.GoodsAboutDto.GoodsType;
import com.qububu.emporium.goods.dto.GoodsInfoDto;
import com.qububu.emporium.goods.dto.GoodsStatus;
import com.qububu.emporium.goods.service.GoodsService;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

/**
 * 功能  管理员对商品的处理
 * @author LH
 */
@Controller
@RequestMapping("/goods")
public class GoodsControl {

	@Resource
	private GoodsService goodsService;
	
	@RequestMapping("/info")
	public String infoEmporium(HttpServletRequest req) {
		
		// 设置购物标记
		String uuid = SystemUtil.getUUIDString();
		HttpSession session = req.getSession();
		session.removeAttribute(session.getId());
		session.setAttribute(SystemConstants.CART_IDENTIFICATION_KEY, uuid);
		req.setAttribute("cartBase", uuid);
		
		return "phoneEmporium";
	}
	
	@RequestMapping("/admin/goodsController")
	public String goodsController() {
		
		return "adminGoods";
	}
	
	
	/**
	 * @功能 添加菜品
	 * @author LH
	 * */
	@RequestMapping("/admin/addGoods")
	@ResponseBody
	public Map<String, Object> addGoods(HttpServletRequest request, HttpServletResponse response) {
//		GoodsInfoDto goodsInfo = makeGoodsInfoDto(request, response);
		String goodsId = SystemUtil.getUUIDString();
		String goodsName = assertNotEmpty(request.getParameter("goodsName"));
		String goodsPicUrl = assertNotEmpty(request.getParameter("goodsPicUrl"));
		String goodsPrice = assertNotEmpty(request.getParameter("goodsPrice"));
		String goodsUseNum = assertNotEmpty(request.getParameter("goodsUseNum"));
		String goodsSummaryPicUrls = assertNotEmpty(request.getParameter("goodsSummaryPicUrls"));
		String goodsDetailsUrl = assertNotEmpty(request.getParameter("goodsDetailsUrl"));
		String goodsType = assertNotEmpty(request.getParameter("goodsType"));
		String goodsPromise = assertNotEmpty(request.getParameter("goodsPromise"));
		
		GoodsInfoDto goodsInfo = new GoodsInfoDto(goodsId);
		int goodsPoint = 5;
		goodsInfo.setGoodsPoint(goodsPoint);
		goodsInfo.setGoodsPromise(goodsPromise);
		goodsInfo.setGoodsName(goodsName);
		goodsInfo.setGoodsPicUrl(goodsPicUrl);
		goodsInfo.setGoodsPrice(goodsPrice);
		goodsInfo.setGoodsUseNum(ParamConverter.convertToInteger(goodsUseNum));
		goodsInfo.setGoodsSummaryPicUrls(goodsSummaryPicUrls);
		goodsInfo.setGoodsDetailsUrl(goodsDetailsUrl);
		goodsInfo.setGoodsType(GoodsType.getType(goodsType));
		goodsInfo.setGoodsStatus(GoodsStatus.NEW);
		
		goodsService.doAddGoods(goodsInfo);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		return resultMap;
	}
	
	/**
	 * @功能 修改菜品
	 */
	@RequestMapping("/admin/updateGoods")
	@ResponseBody
	public Map<String, Object> updateGoods(HttpServletRequest request, HttpServletResponse response){
		GoodsInfoDto goodsInfo = makeGoodsInfoDto(request, response);
	
		goodsService.doUpdateGoods(goodsInfo);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		return resultMap;
	}
	
	/**
	 * @功能 删除菜品
	 * @author LH
	 * */
	@RequestMapping("/admin/ajaxDeleteGoods")
	@ResponseBody
	public Map<String, Object> ajaxDeleteGoods(HttpServletRequest request, HttpServletResponse response) {
		String goodsId = assertNotEmpty(request.getParameter("gi"));
		
//		goodsService.doDeleteGoods(goodsId);
		goodsService.doChangeGoodsStatus(goodsId, GoodsStatus.DELETED.toIntValue());
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		
		return resultMap; 
	}
	
	/**
	 * @author HQ
	 * @功能 根据商品id查询商品信息
	 * @param 
	 */
	@RequestMapping("/showGoodsInfo")
	@ResponseBody
	public Map<String, Object> searchGoodsInfoById(HttpServletRequest request){
		
		Map<String, Object> goodsInfoMap = new HashMap<String, Object>();
		String goodsId = assertNotEmpty(request.getParameter("goodsId"));
		GoodsInfoDto goodsInfo = goodsService.getGoodsInfoById(goodsId);
		goodsInfoMap.put("result", SystemConstants.AJAX_SUCCESS);
		goodsInfoMap.put("goodsInfo", goodsInfo);
		
		return goodsInfoMap;
	}
	
	/**
	 * @author ZCW
	 * @功能 根据商品类型ID查出商品列表
	 * */
	@RequestMapping("/getGoods")
	@ResponseBody
	public Map<String, Object> getGoodsByType(HttpServletRequest req) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String goodsType = assertNotEmpty(req.getParameter("goodsType"));
		List<GoodsAboutDto> goodsList = goodsService.getGoodsListByType(GoodsType.getType(goodsType).toIntValue());
		resultMap.put("goodsList", goodsList);
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		
		return resultMap;
	}
	
	private GoodsInfoDto makeGoodsInfoDto(HttpServletRequest request, HttpServletResponse response) {
		String goodsId = assertNotEmpty(request.getParameter("goodsId"));
		String goodsName = assertNotEmpty(request.getParameter("goodsName"));
		String goodsPicUrl = assertNotEmpty(request.getParameter("goodsPicUrl"));
		String goodsPrice = assertNotEmpty(request.getParameter("goodsPrice"));
		String goodsUseNum = assertNotEmpty(request.getParameter("goodsUseNum"));
		String goodsSummaryPicUrls = assertNotEmpty(request.getParameter("goodsSummaryPicUrls"));
		String goodsDetailsUrl = assertNotEmpty(request.getParameter("goodsDetailsUrl"));
		String goodsType = assertNotEmpty(request.getParameter("goodsType"));
		String goodsPromise = assertNotEmpty(request.getParameter("goodsPromise"));
		
		GoodsInfoDto goodsInfo = new GoodsInfoDto(goodsId);
		int goodsPoint = 5;
		goodsInfo.setGoodsPoint(goodsPoint);
		goodsInfo.setGoodsPromise(goodsPromise);
		goodsInfo.setGoodsName(goodsName);
		goodsInfo.setGoodsPicUrl(goodsPicUrl);
		goodsInfo.setGoodsPrice(goodsPrice);
		goodsInfo.setGoodsUseNum(ParamConverter.convertToInteger(goodsUseNum));
		goodsInfo.setGoodsSummaryPicUrls(goodsSummaryPicUrls);
		goodsInfo.setGoodsDetailsUrl(goodsDetailsUrl);
		goodsInfo.setGoodsType(GoodsType.getType(goodsType));
		
		return goodsInfo;
	}
}
